#!/usr/bin/python2.4
from BGP import Update
from Lib import Util, UpdateDBHelper
import bz2, sys, os, datetime, string, time

# Generate a datetime object given a list of
# [year, month, day] that can be turned into ints
def get_dt(ymd, hms = (0, 0, 0)):
    return datetime.datetime(int(ymd[0]), int(ymd[1]), int(ymd[2]), int(hms[0]), int(hms[1]), int(hms[2]))

# Check command line arg length and exit on something unexpected
if len(sys.argv) < 4:
    print 'Usage: %s YYYY-MM-DD YYYY-MM-DD monitor' % sys.argv[0]
    sys.exit(1)
    
# Initialize command line arguments
# No syntax checking or anything
sd = get_dt(string.split(sys.argv[1], '-'),(0, 0, 0))
ed = get_dt(string.split(sys.argv[2], '-'),(23, 59, 59))
assert(ed > sd)

monitor = sys.argv[3]
prefixIds = {}
os.umask(002)
    
line_num = 0
problem_lines = 0
updateDBHelper = UpdateDBHelper.UpdateDBHelper(None, monitor)
updateDBHelper.checkDates(sd, ed)

lastTime = time.time()
print "Loading archive list...",
archives = Util.get_plain_list(sd, ed, monitor)
print "done (%s)" % (time.time() - lastTime)
sys.stdout.flush()
lastTime = time.time()

#print "Disabling keys...",
#sys.stdout.flush()
#updateDBHelper.disableKeys()
#print "done (%s)" % (time.time() - lastTime)
#sys.stdout.flush()
#lastTime = time.time()

print "Processing files (each date is 5000000 lines):"
fileLine = None
curMonth = None
curYear = None

for file in archives:
    fh = bz2.BZ2File(file, 'r')
    lines = fh.readlines()
    for line in lines:
        line_num += 1
            
        try:
            update = Update.Update(line)
        except:
            problem_lines += 1
            continue

        date = time.gmtime(update.time)
        if date[1] != curMonth:
            curMonth = date[1]
            curYear = date[0]
            updateDBHelper.setTable(curMonth, curYear)
            
        if (line_num % 5000000 == 0):           
            print "%s-%s-%s %s:%s:%s (%s)" % (date[0], date[1], date[2], date[3], date[4], date[5], time.time() - lastTime)
            lastTime = time.time()
            sys.stdout.flush()
            
        if update.type == 'A' or update.type == 'W':            
            for prefix in update.prefix:
                if prefix not in prefixIds:
                    prefixIds[prefix] = updateDBHelper.getPrefixId(prefix)
                updateDBHelper.addUpdate(prefixIds[prefix], update)

updateDBHelper.commit()
print "All updates committed, creating indexes, table should now index (%s)..." % (time.time())
sys.stdout.flush()
updateDBHelper.createIndices()
print " done (%s)" % (time.time() - lastTime)

updateDBHelper.setDates(sd, ed)
updateDBHelper.commit()

print "\nRead %s lines, %s had problems. %s total prefixes" % (line_num, problem_lines, len(prefixIds))